<?php

class News {
    public static $pagesize = 10;

    /**
     * 取第一个大图片文章
     * @param $args
     */
    public static function getNewsTop($args)
    {

    }
    public static function getNewsCategory($paras)
    {
        global $db;
        global $redis;
        global $utility;

        $url_args = array('cid|N|0');
        $paras = $utility->get_url_args($paras, $url_args);

        $redis_key = \Config\Sys::$redisKey['NCATE'] . $paras['cid'];
        if($redis->exists($redis_key))
        {
            $datas = $utility->redisGet($redis_key);
        }else {
            $helpid = $utility->getSystemParams('helpid');
            $where_txt = '`state`=1 AND `id`<>'.intval($helpid);
            //需要处理的页面参数，用来翻页操作

            $tablename = \Config\Sys::$dbprefix . 'news_category';
            $fields = "`id`,`title`";
            $sql = "SELECT ~field~ FROM `{$tablename}` WHERE {$where_txt} ~order~ ~limit~";

            $orderby = " ORDER BY `sort` ASC";

            //取出当前页的数据
            $sql_arr = array(
                'field' => $fields,
                'page_current' => 1,
                'pagesize' => 100,
                'order' => $orderby,
            );
            $datas = $utility->getTableRows($sql, $sql_arr);
            $utility->redisSet($redis_key, $datas);
        }
        return $datas;

    }
    /**
     * 取分类数据
     * @param $category int 分类的ID
     */
    public static function getNewsList($args)
    {
        global $db;
        global $redis;
        global $utility;

        $paras = $args;

        $cid = $utility->existsKey('cid',$paras,'N');
        $keys = $utility->existsKey('keys',$paras,'S');
        $curPage = $utility->existsKey('pc',$paras,'N',1);


        $redis_key = \Config\Sys::$redisKey['NLIST'] . $cid .'_'. $curPage;
        if($redis->exists($redis_key))
        {
            $datas = $utility->redisGet($redis_key);
        }else {
            $db_prefix = \Config\Json::$config['db_prefix'];
            $where_txt = " `state`=1";
            //需要处理的页面参数，用来翻页操作
            $url_args = array("ps","pc",'keys', 'cid');
            $get_args = $utility->get_url_args($paras, $url_args);
            $page_args = $utility->get_page_args($paras, $url_args);

            $_pagesize = $utility->existsKey('ps',$paras,'N', self::$pagesize);
            $_currentPage = $utility->existsKey('pc',$paras,'N', 1);

            //$begintimes = strtotime(date('Y-m-d'));
            //$endtimes = $begintimes + 86400;

            if(!empty($cid)) $where_txt .= " AND `category`=".$cid;
            if(!empty($keys)) $where_txt .= " AND `title` LIKE '%".$keys."%'";

            $tablename = \Config\Json::$config['db_prefix'] . 'news';
            $fields = "`id`,`title`,REPLACE(`icon`,'/upload/','') as icon,REPLACE(`icon1`,'/upload/','') as icon1,REPLACE(`icon2`,'/upload/','') as icon2,`counticon`,FROM_UNIXTIME(`actiontime`) AS actiontime,`reading`,`url`,`isbig`,`validity`,`subtitle`,`explain`";
            $sql = "SELECT ~field~ FROM `{$tablename}` WHERE {$where_txt} ~order~ ~limit~";

            $orderby = " ORDER BY `sort` DESC";

            //取出当前页的数据
            $sql_arr = array(
                'field' => $fields,
                'page_current' => $_currentPage,
                'pagesize' => $_pagesize,
                'order' => $orderby,
            );
            $datas = $utility->getTableRows($sql, $sql_arr);
            # 取出分类标题
            $sql  = "SELECT `title` FROM `".\Config\Sys::$dbprefix."news_category` WHERE `id`=".$paras['cid'];
            $title = $db->row($sql);
            $datas['title'] = $title['title'];
            $datas = [
                'state' => 200,
                'datas' => $datas['datas']
            ];
            $utility->redisSet($redis_key, $datas);
        }
        return $datas;
    }

    /**
     * 取单条新闻数据
     * @param $paras
     */
    public static function getNewsItem($paras)
    {
        global $db;
        global $utility;

        $mastArgs = array('id|N');
        $paras = $utility->get_url_args($paras, $mastArgs);
        $redis_key = \Config\Sys::$redisKey['NITEM'] . $paras['id'];
        $datas = $utility->redisGet($redis_key);
        if(!$datas){
            $where = "n.`id`={$paras['id']} AND c.`newsid`={$paras['id']}";
            $fields = "n.`id`,n.`title`,`validity`,`subtitle`,FROM_UNIXTIME(n.`actiontime`) AS actiontime,c.`content`";
            $tablename = '`'.\Config\Sys::$dbprefix.'news` n,`'.\Config\Sys::$dbprefix.'news_content` c';
            $sql = "SELECT {$fields} FROM {$tablename} WHERE {$where} LIMIT 1";
            $datas = $db->row($sql);
            $datas = array(
                'state' => 200,
                'actiontime' => $datas['actiontime'],
                'content' => $datas['content'],
                'id' => $datas['id'],
                'subtitle' => $datas['subtitle'],
                'title' => $datas['title'],
                'validity' => $datas['validity'],
            );
            $utility->redisSet($redis_key, $datas);
        }
        $db->query('UPDATE '.\Config\Sys::$dbprefix.'news SET reading=reading+1 WHERE id='.$paras['id'].' LIMIT 1');
        return $datas;
    }
}